package com.yyds.blackmagic.controller;

import com.yyds.blackmagic.entity.AdminMenu;
import com.yyds.blackmagic.entity.AdminPermission;
import com.yyds.blackmagic.entity.AdminRole;
import com.yyds.blackmagic.service.AdminMenuService;
import com.yyds.blackmagic.service.AdminPermissionService;
import com.yyds.blackmagic.service.AdminRoleService;
import com.yyds.blackmagic.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author ccc
 * @Date 2021/12/17
 * @Descripton
 */
@RestController
@RequestMapping("/api")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AdminMenuService adminMenuService;
    @Autowired
    AdminPermissionService adminPermissionService;
    @GetMapping("/admin/role")
    public ActionResult allRoles(){
        List<AdminRole> all = adminRoleService.findAll ();
        return new ActionResult (200,"admint_role",all);
    }

    @PostMapping("/admin/role")
    public ActionResult addRole(@RequestBody AdminRole requestRole) {
        AdminRole adminRole =  adminRoleService.add(requestRole);
        return new ActionResult (200,"添加角色成功",adminRole);
    }
    @PutMapping("/admin/role")
    public ActionResult editRole(@RequestBody AdminRole requestRole) {
        int res =  adminRoleService.update(requestRole);
        int res1 =  adminPermissionService.update(requestRole.getId(), requestRole.getPerms());
        String message = "修改角色信息成功";
        return new ActionResult (200,message,null);
    }


    @GetMapping("/admin/role/menu")
    public ActionResult listAllMenus(){
        List<AdminMenu> all = adminMenuService.getMenusByRoleId (1) ;
        return new ActionResult (200,"admint_role",all);
    }

    @GetMapping("/admin/role/perm")
    public ActionResult listAllRules(){
        List<AdminPermission> all = adminPermissionService.findAll ();
        return new ActionResult (200,"admint_role",all);
    }

    @PutMapping("/admin/role/status")
    public ActionResult updateStatus(@RequestBody AdminRole requestRole) {
       Integer res = adminRoleService.updateRoleStatus(requestRole);
        String message = "用户状态更新成功";
        return new ActionResult (200,message,null);
    }

    @PutMapping("/admin/role/menu")
    public ActionResult updateRoleMenu(@RequestParam int rid, @RequestBody Map<String, List<Integer>> menusIds) {
        adminRoleService.updateRoleMenu(rid, menusIds);
        return new ActionResult (200,"更新成功",null);
    }
}
